// Copyright (c) 2011, Marc-Andre Ferland - madrang@gmail.com
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the The Warrent Team nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//     * This software is free for non-commercial use. You may not use this
//       software, in whole or in part, in support of any commercial product
//       without the express consent of the author.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE WARRENT TEAM BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using System;
using System.Runtime.InteropServices;

namespace OsHelper.UsbNet.Descriptors
{
	[StructLayout(LayoutKind.Explicit, Size = 2)]
	public struct BcdVersion
	{
		[FieldOffset(0)]
		private ushort ush_Value;
		
		private BcdVersion (ushort Value)
		{
			this.ush_Value = Value;
		}
		
		public BcdVersion (int MyMajor, int MyMinor, int MySubMinor)
		{
			this.Major = MyMajor;
			this.Minor = MyMinor;
			this.SubMinor = MySubMinor;
		}
		
		public static explicit operator ushort(BcdVersion a)
		{
			return a.ush_Value;
		}
		
		public static explicit operator BcdVersion(ushort a)
		{
			return new BcdVersion (a);
		}
		
		public static explicit operator short(BcdVersion a)
		{
			return (short)a.ush_Value;
		}
		
		public static explicit operator BcdVersion(short a)
		{
			return new BcdVersion ((ushort)a);
		}
		
		public static explicit operator Version(BcdVersion a)
		{
			return new Version(a.Major, a.Minor, a.SubMinor);
		}
		
		public static explicit operator BcdVersion(Version a)
		{
			return new BcdVersion(a.Major, a.Minor, a.Build);
		}
		
		public int Major {
			get {
				return (this.ush_Value & 0xFF00) >> 8;
			}
			set {
				if((value & ~0x00FF) != 0)
					throw new ArgumentOutOfRangeException("value");
				
				this.ush_Value |= (ushort)((value & 0xFF) << 8);
			}
		}
		
		public int Minor {
			get {
				return (this.ush_Value & 0x00F0) >> 4;
			}
			set {
				if((value & ~0x000F) != 0)
					throw new ArgumentOutOfRangeException("value");
				
				this.ush_Value |= (ushort)((value & 0x000F) << 4);
			}
		}
		
		public int SubMinor {
			get {
				return (this.ush_Value & 0x000F);
			}
			set {
				if((value & ~0x000F) != 0)
					throw new ArgumentOutOfRangeException("value");
				
				this.ush_Value |= (ushort)(value & 0x000F);
			}
		}
		
	}
}
